package com.smedi.ismedi.authoritymanagement.core.port.adapter.restful;

import com.smedi.ismedi.authority.starter.annotation.SkipAuthority;
import com.smedi.ismedi.authority.starter.response.AuthorityAccessResponse;
import com.smedi.ismedi.authoritymanagement.api.dto.request.LoginRequest;
import com.smedi.ismedi.authoritymanagement.api.dto.response.NodeInfoResponse;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteClientLoginFeignApi;
import com.smedi.ismedi.authoritymanagement.core.application.service.ClientLoginApplicationService;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.AuthorityAccess;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.NodeInfo;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.service.PersonnelRemoteApiService;
import com.smedi.ismedi.database.starter.annotation.SkipSqlPermission;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xulinjun
 * @Date: 2021-7-2 20:17
 */
@Slf4j
@RestController
@AllArgsConstructor
public class ClientLoginController implements RemoteClientLoginFeignApi {
    private final ClientLoginApplicationService clientLoginApplicationService;
    private final PersonnelRemoteApiService personnelRemoteApiService;

    private final DozerUtil dozerUtil;

    /**
     * {@inheritDoc}
     */
    @Override
    @SkipAuthority
    @SkipSqlPermission
    public ResponseData<AuthorityAccessResponse> postLoginPc(LoginRequest loginRequest) {
        AuthorityAccess authorityAccess = clientLoginApplicationService.pcLogin(loginRequest.getAccount(), loginRequest.getPassword(), loginRequest.getClientId());
        personnelRemoteApiService.getLoginScopeInitializeDataAndSubmit();

        return new ResponseData<AuthorityAccessResponse>().success(dozerUtil.convertor(authorityAccess, AuthorityAccessResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SkipAuthority
    @SkipSqlPermission
    public ResponseData<AuthorityAccessResponse> postLoginMobile(LoginRequest loginRequest) {
        AuthorityAccess authorityAccess = clientLoginApplicationService.mobileLogin(loginRequest.getAccount(), loginRequest.getPassword(), loginRequest.getClientId());
        personnelRemoteApiService.getLoginScopeInitializeDataAndSubmit();

        return new ResponseData<AuthorityAccessResponse>().success(dozerUtil.convertor(authorityAccess, AuthorityAccessResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> putLogout(String token) {
        clientLoginApplicationService.loginOut(token);
        return new ResponseData<Boolean>().success(true);
    }

    @Override
    public ResponseData<AuthorityAccessResponse> postLoginPcFast(LoginRequest loginRequest) {
        AuthorityAccess authorityAccess = clientLoginApplicationService.pcLoginFast(loginRequest.getAccount(), loginRequest.getPassword());
        return new ResponseData<AuthorityAccessResponse>().success(dozerUtil.convertor(authorityAccess, AuthorityAccessResponse.class));
    }

    @Override
    public ResponseData<AuthorityAccessResponse> postLoginMobileFast(LoginRequest loginRequest) {
        AuthorityAccess authorityAccess = clientLoginApplicationService.mobileLoginFast(loginRequest.getAccount(), loginRequest.getPassword(),loginRequest.getClientId());
        return new ResponseData<AuthorityAccessResponse>().success(dozerUtil.convertor(authorityAccess, AuthorityAccessResponse.class));
    }

    @Override
    public ResponseData<NodeInfoResponse> getInitScopeTopPermissionInfo(String device) {
        NodeInfo nodeInfo = clientLoginApplicationService.getInitScopeTopPermissionInfo(device).getNodeInfo();
        NodeInfoResponse nodeInfoResponse = dozerUtil.mapGeneric(nodeInfo, NodeInfoResponse.class);
        return new ResponseData<>().success(nodeInfoResponse);
    }

    @Override
    public ResponseData<NodeInfoResponse> getCurrentScopeTopPermissionInfo() {
        NodeInfo nodeInfo = clientLoginApplicationService.getCurrentScopeTopPermissionInfo().getNodeInfo();
        NodeInfoResponse nodeInfoResponse = dozerUtil.mapGeneric(nodeInfo, NodeInfoResponse.class);
        return new ResponseData<>().success(nodeInfoResponse);
    }
}
